﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class back_ViewUser : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    User registration;
    PaymentHistory pHistory;
    List<User> lstRegistration = new List<User>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindUserGrid();
        }
    }

    private void bindUserGrid()
    {
        registration = new User(_dataObject);
        lstRegistration = registration.GetAllUsers();

        if (lstRegistration.Count > 0)
        {
            grdUser.Visible = true;
            grdUser.DataSource = lstRegistration;
            grdUser.DataBind();
        }
        else
        {
            grdUser.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdUser_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        grdUser.PageIndex = e.NewPageIndex;
        bindUserGrid();
    }
    protected void grdUser_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower() == "editmenu")
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }
        if (e.CommandName.ToString().ToLower().Trim().Equals("deletemenu"))
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnUserId = (System.Web.UI.WebControls.Label)row.FindControl("hdnUserId");
            registration = new User(_dataObject);
            registration.Id = Convert.ToInt32(hdnUserId.Text);
            registration.Delete();
            bindUserGrid();
            divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdUser_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label hdnUserId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnUserId");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnEdit");
            System.Web.UI.WebControls.Button btnPayment = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnPayment");
            btnEdit.PostBackUrl = "AddUser.aspx?id=" + hdnUserId.Text;
            btnPayment.Attributes.Add("OnClick", "lanchPopUp(" + hdnUserId.Text + "); return false;");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        pHistory = new PaymentHistory(_dataObject);
        pHistory.Amount = Convert.ToDecimal(txtAmount.Text);
        pHistory.Mode = drpMode.SelectedItem.Value;
        pHistory.UserId = Convert.ToInt32(lblUserId.Text);
        pHistory.Date = System.DateTime.Now;
        pHistory.Create();
    }
}